package com.xzy.sms.web.domain.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.GenericGenerator;

import com.xzyframework.persistence.BaseEntity;

/**
 * @desc 短信发送通道
 * @author wjw
 * @time 2017年11月16日下午3:31:33
 */
@Entity
@Table(name = "pub_channel_config")
public class ChannelConfig extends BaseEntity {
    
    /** 序列化 */
    private static final long serialVersionUID = -936203994181527906L;
    
    /** 主键id，也是applicationId */
    @Id
    @GeneratedValue(generator = "UIDGenerator")
    @GenericGenerator(name = "UIDGenerator", strategy = "com.xzyframework.persistence.UIDGenerator")
    @Column(length = 40)
    private String channelConfigId;
    
    /** 通道名称 */
    @NotNull(message = "通道名不能为空")
    @NotBlank(message = "通道名不能为空")
    @Column(length = 40)
    private String title;
    
    /** 通道名称 */
    @NotNull(message = "通道适配器类不能为空")
    @NotBlank(message = "通道适配器类不能为空")
    @Column(length = 200)
    private String sendAdapterClass;
    
    /** 通道描述 */
    @Column(length = 200)
    private String description;
    
    /** 短信网关的地址 ，如果是数据库则为连接字符串 */
    @NotNull(message = "短信网关地址不能为空")
    @NotBlank(message = "短信网关地址不能为空")
    @Column(length = 500)
    private String url;
    
    /** 短信网关账号 */
    @Column(length = 100)
    private String account;
    
    /** 短信网关密码 */
    @Column(length = 100)
    private String password;
    
    /** 扩展参数 */
    @Column(length = 1000)
    private String parameters;
    
    /**
     * 构造函数
     */
    public ChannelConfig() {
    }
    public String getChannelConfigId() {
        return channelConfigId;
    }
    public void setChannelConfigId(String channelConfigId) {
        this.channelConfigId = channelConfigId;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    
    /**
     * 获取指定的扩展参数值
     * @param key xx
     * @return the parameters
     */
    public String getParameter(String key) {
        
        if (key == null || key.length() == 0 || parameters == null || parameters.length() == 0) {
            return null;
        }
        String[] pks = StringUtils.split(parameters.trim(), ";");
        if (pks == null || pks.length == 0) {
            return null;
        }
        for (String pk : pks) {
            String[] kv = StringUtils.split(pk.trim(), "=");
            if (kv != null && kv.length == 2 && StringUtils.equals(key, kv[0].trim())) {
                return kv[1].trim();
            }
        }
        return null;
    }
    public String getParameters() {
        return parameters;
    }
    public void setParameters(String parameters) {
        this.parameters = parameters;
    }
    public String getSendAdapterClass() {
        return sendAdapterClass;
    }
    public void setSendAdapterClass(String sendAdapterClass) {
        this.sendAdapterClass = sendAdapterClass;
    }
    
    /**
     * @return
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "ChannelConfig [channelConfigId=" + channelConfigId + ", title=" + title + ", sendAdapterClass=" + sendAdapterClass + ", description="
            + description + ", url=" + url + ", account=" + account + ", password=" + password + ", parameters=" + parameters + "]";
    }
    
}
